package com.glorysoft.tonglingxjpda.ui.function.viewmodel

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseViewModel
import com.glorysoft.tonglingxjpda.base.Constant
import com.glorysoft.tonglingxjpda.entity.BomEntity
import com.glorysoft.tonglingxjpda.entity.KyhEntity
import com.glorysoft.tonglingxjpda.entity.net.HeaderBean
import com.glorysoft.tonglingxjpda.entity.net.NetRequestBean
import com.glorysoft.tonglingxjpda.entity.net.ReturnBean
import com.glorysoft.tonglingxjpda.model.TaskRepository
import com.glorysoft.tonglingxjpda.ui.function.adapter.KyhAdapter
import com.glorysoft.tonglingxjpda.utils.FileUtils
import com.glorysoft.tonglingxjpda.utils.SmbFileUtils
import com.glorysoft.tonglingxjpda.utils.SmbFileUtils.remoteUrl
import com.glorysoft.tonglingxjpda.utils.Tip
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils
import jcifs.smb.SmbFile
import org.apache.commons.lang3.StringEscapeUtils
import java.io.File
import java.lang.NumberFormatException
import java.util.HashMap
import kotlin.concurrent.thread


/**
 * FileName: KYHInfoViewModel
 * Author: XJH
 * Date: 2021/7/16 18:06
 * Description:
 * History:
 */
class KYHInfoViewModel : BaseViewModel() {

    val kyhListTitle = arrayListOf("序号", "原厂料号", "刻印号","原厂印号","RF TIME", "累计RF TIME","部品描述", "产品名")
    private val kyhRepository = TaskRepository()
    var partAdapter: KyhAdapter? = null
    private val handler = Handler(Looper.getMainLooper())

    fun initPart(po: String, entity: BomEntity) {
        partAdapter = KyhAdapter(arrayListOf(), po, entity.PARTNAME,entity.RESERVED7)
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "COMPONENTUMITS")
        map.addProperty("DOCID", po)
        map.addProperty("PARTNAME", entity.PARTNAME)
        val json = JsonObject()
        json.addProperty("ACTIONTYPE", "COMPONENTUMIT")
        json.addProperty("DOCID", po)
        json.addProperty("PARTNAME", entity.PARTNAME)
        launchOnlyResult({
            val hint = kyhRepository.getHint(json)
            partAdapter!!.hintList.clear()
            if (hint.COMPONENTUNIT.isNotEmpty()) {
                hint.COMPONENTUNIT.forEach {
                    partAdapter!!.hintList.add(it.COMPONENTALIAS)
                }
            }
            val kyh = kyhRepository.getKyh(map)
            if (kyh.COMPONENTUNIT.isEmpty()){
                addLocalData(entity)
            }else{
                partAdapter?.setNewData(kyh.COMPONENTUNIT)
            }

        })
    }

    private fun addLocalData(entity: BomEntity) {
        if (entity.MAINQTY.isNotEmpty()){
            try {
                val toInt = entity.MAINQTY.toInt()
                for (index in 1..toInt){
                    var data = KyhEntity.ComponentEntity()
                    data.POSITION = index.toString()
                    data.PARTNAME = entity.PARTNAME
                    data.MASK = entity.RESERVED8
                    data.PARTDESC = entity.WORKORDERTYPE
                    partAdapter?.addData(data)
                }
            }catch (e : NumberFormatException){
                e.printStackTrace()
            }
        }
    }

    fun saveKyh(po: String, name: String, path: String,actiontype: String) {
        val fileName = name + FreeApi_DateUtils.getNowStringDate("yyyyMMddHHmmssSSS")
        if (path.isEmpty()){
            save(po,name,path,actiontype)
        }else{
            thread {
                if (SmbFileUtils.smbUpload(
                        path,
                        fileName
                    )
                ) {
                    save(po,name,"$remoteUrl$fileName.jpg",actiontype)
                }else{
                    handler.post {
                        defUI.toastEvent.value = ToastMessageBean("图片上传失败",false)
                        defUI.callObserve.value = CallObserveBean(0,"FAIL")
                    }
                }
            }
        }




    }

    private fun save(po: String, name: String, path: String,actiontype :String){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", actiontype)
        map.addProperty("DOCID", po)
        map.addProperty("PARTNAME", name)
        partAdapter!!.data.forEach {
            it.PIC = path
        }
        map.add(
            "COMPONENTUNIT",
            JsonParser().parse(Gson().toJson(partAdapter?.data)).asJsonArray
        )
        launchOnlyResult({
            kyhRepository.saveKyh(map)
            defUI.toastEvent.value = ToastMessageBean("保存成功", true)
            defUI.startActivity.call()
        })
    }

}